package cn.avalon.controller;

import cn.avalon.domain.User;
import cn.avalon.downgrade.DowngradeUtil;
import cn.avalon.downgrade.FuseDowngradeUtil;
import cn.avalon.feign.UserFeignClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: Avalon
 * @date: 2022年09月21日 ： 11:25
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/{id}")
    @SentinelResource(value = "order-service", blockHandler = "orderBlockHandler", blockHandlerClass = DowngradeUtil.class, fallback = "fuseDegradation", fallbackClass = FuseDowngradeUtil.class)
    public User findById(@PathVariable("id") Long id){
        // 调用OpenFeign接口，通信User服务
        int a = 1 / 0;
        return userFeignClient.findById(id);
    }

//    public User orderBlockHandler(@PathVariable("id") Long id, BlockException e){
//        // 调用OpenFeign接口，通信User服务
//        return new User(-1L, "服务降级触发！", "当前服务流量过大，请稍后重试！");
//    }

    /*
     * @Description: 熔断降级方法
     * @Author: Director
     * @Date: 2022/7/31 1:50
     * @param id:
     * @param e: 降级异常
     * @return:
     **/
//    public User fuseDegradation(@PathVariable("id") Long id, Throwable  e){
//        e.printStackTrace();
//        return new User(-1L, "熔断服务降级触发！", "服务不可用，请稍后重试！");
//    }
}
